#include <signal.h>
#include <setjmp.h>
#include <stdio.h>

static jmp_buf env_alrm;

static void
sig_alrm(int signo){
    longjmp(env_alrm, 1);
}

unsigned int
sleep2(unsigned int nsecs){
    if(signal(SIGALRM, sig_alrm) == SIG_ERR)
        return(nsecs);

    if(setjmp(env_alrm) == 0){
        alarm(nsecs);
        pause();
        printf("after\n"); /* should not come here */
    }

    return(alarm(0));
}

int
main(){
    printf("sleep 1 secs\n");
    sleep2(1);
}
